package com.whlilong.jxc.sys;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Regist;
import com.whlilong.jxc.ui.RegistDialog;
import com.whlilong.jxc.util.FileUtil;

public class Register {

	boolean isSuccessful = false;

	public Register() {
		// TODO Auto-generated constructor stub
	}

	public boolean isRegist() {

		final RegistDialog registDialog = new RegistDialog(Display.getCurrent()
				.getActiveShell());

		while (!isSuccessful) {
			int code = registDialog.open();
			if (code != Window.OK) {
				return false;
			}

			String registcode = SaveLogInfo.getInstance().getRegistcode();
			if (registcode.equals("")) {
				MessageDialog.openWarning(null, "提示", "请输入注册店名进行注册，以后注册请点试用！");
				isSuccessful = false;
			} else {
				byte[] bytes = registcode.getBytes();
				StringBuffer strBuffer = new StringBuffer();
				for (int k = 0; k < bytes.length; k++) {
					strBuffer = strBuffer.append(Integer.toHexString(
							bytes[k] & 0xff).toUpperCase());
					if (k % 2 == 0)
						strBuffer.append("-");
				}
				String recode = strBuffer.substring(0, strBuffer.length() - 1);
				System.out.print(recode);
				List<Regist> list = ExtentionInfo.getInstance().getRegist();
				Regist rg = list.get(0);
				rg.setRegistcode(recode);
				DbManager.getInstance().update(rg);
				// String registcode =
				// FileUtil.readTxt(FileUtil.getDogPath()+"Register.txt", "");

				isSuccessful = isValidate(recode);
				if (!isSuccessful) {
					MessageDialog.openWarning(null, "提示", "注册码错误！");
				} else
					MessageDialog.openWarning(null, "提示", "注册成功！");
			}

		}
		return isSuccessful;
	}

	private boolean isValidate(final String recode) {
		// TODO Auto-generated method stub
		
		String registcode =FileUtil.readRegistcode(recode);
		if (registcode.trim().equals(recode))
			return true;
		else
			return false;
	}

}
